package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetMealService.class)
public class SetMealServiceImpl implements SetMealService {


    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    @Transactional
    public void add(int[] ids, Setmeal setmeal) {

        setMealMapper.addSetmeal(setmeal);

        if (ids != null && ids.length > 0) {
            for (int gid : ids) {
                setMealMapper.setMealAndCheckGroup(setmeal.getId(), gid);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page=setMealMapper.queryByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }


    public List<Setmeal> findAllSetMeal() {

        return setMealMapper.findAllSetMeal();
    }

    @Override
    public Setmeal findById(int id) {
        return setMealMapper.findById(id);
    }

    @Override
    public List<Map<String, Object>> findSetMealCount() {

        List<Map<String, Object>> list=setMealMapper.findSetMealCount();
        return list;
    }
}
